﻿var codeContext, $elementContext;
function LoadContext() {
    $('#stockList tbody tr').bind("contextmenu", function (event) {
        $elementContext = $(event.currentTarget);
        codeContext = $elementContext.attr('tcode');
        // Avoid the real one
        event.preventDefault();
        $(".contextMenu").finish().toggle(100).
        css({
            top: event.pageY + "px",
            left: event.pageX + "px"
        });
    });
    // If the document is clicked somewhere
    $(document).bind("mousedown", function (e) {
        // If the clicked element is not the menu
        if (!$(e.target).parents(".contextMenu").length > 0) {
            // Hide it
            $(".contextMenu").hide(100);
        }
    });
}

var processing = false;
var iiCount = 0;
$(function () {
    $('body').on('click', '.createfav', function () {
        OpenAddFavorite();
    })

    $('body').on('click', '#btnAddFav', function () {
        $FavoriteName = $('#FavoriteName');
        var favName = $FavoriteName.val();
        if (favName.trim().length > 5) {
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { favName: favName, type: 'add' },
                dataType: "json",
                success: function (response) {
                    if (response.Status == 1) {
                        var html = '<tr data-fid="' + response.FavID + '"><td>' + favName + '</td><td><i class="glyphicon glyphicon-remove" aria-hidden="true"></i></td></tr>';
                        $('#favList tbody').append(html);
                        $FavoriteName.val('');
                        RefreshContext();
                    }
                }
            })
        }
        else {
            DisplayMessage(2, lblRequiredMinFavouriteName);
            $FavoriteName.focus();
        }
    })

    $('#favList').on('click', '.glyphicon-remove', function () {
        if (processing == false) {
            processing = true;
            var parentElement = $(this).parent().parent();
            var favID = parentElement.data('fid');
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { favID: favID, type: 'delete' },
                dataType: "json",
                success: function (response) {
                    if (response.Status == 1) {
                        parentElement.remove();
                        RefreshContext();
                    }
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //add to watchlist buy
    $('body').on('click', '.contextMenu .addbuy', function () {
        if (processing == false) {
            processing = true;
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'addbuy' },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //them thanh cong
                        $(".contextMenu").finish().toggle(100);
                        DisplayMessage(4, lblAddStockToWatchListBuySuccess);
                    }
                    else if (status == -1) //da ton tai trong watch buy                    
                        DisplayMessage(2, lblThisStockAlreadyExistsOnWatchlistBuy);
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //add to watchlist sell
    $('body').on('click', '.contextMenu .addsell', function () {
        if (processing == false) {
            processing = true;
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'addsell' },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //them thanh cong
                        $(".contextMenu").finish().toggle(100);
                        DisplayMessage(4, lblAddStockToWatchListSellSuccess);
                    }
                    else if (status == -1) //da ton tai trong watch buy
                        DisplayMessage(2, lblThisStockAlreadyExistsOnWatchlistSell);
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })

        }
    })
    //remove from watchlist buy
    $('body').on('click', '.contextMenu .deletebuy', function () {
        if (processing == false) {
            processing = true;
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'deletebuy' },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //xoa thanh cong
                        $(".contextMenu").finish().toggle(100);
                        $elementContext.remove();
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    }
                    else if (status == -1) //khong co trong danh sach buy
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //remove from watchlist sell
    $('body').on('click', '.contextMenu .deletesell', function () {
        if (processing == false) {
            processing = true;
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'deletesell' },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //xoa thanh cong
                        $(".contextMenu").finish().toggle(100);
                        $elementContext.remove();
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    }
                    else if (status == -1) //khong co trong danh sach buy
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //add to fav
    $('body').on('click', '.contextMenu .add2fav', function (e) {
        if (processing == false) {
            processing = true;
            var $element = $(this);
            var favID = $element.data('favid');
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'add2fav', favID: favID },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //add thanh cong
                        $(".contextMenu").finish().toggle(100);
                        DisplayMessage(4, String.format(lblStockSuccessfullyAddTo, $element.text()));
                    }
                    else if (status == -1) //da ton tai
                        DisplayMessage(2, String.format(lblThisStockAlreadyExiston, $element.text()));
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //delete fav item
    $('body').on('click', '.contextMenu .deletefavitem', function () {
        if (processing == false) {
            processing = true;
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'deletefavitem', favID: typeStock },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //xoa thanh cong
                        $(".contextMenu").finish().toggle(100);
                        $elementContext.remove();
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    }
                    else if (status == -1) //khong co trong danh sach buy
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //delet stock
    $('body').on('click', '.contextMenu .deletestock', function () {
        if (processing == false) {
            processing = true;
            var $element = $(this);
            var favID = $element.data('favid');
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'deletestock' },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //add thanh cong
                        $(".contextMenu").finish().toggle(100);
                        $elementContext.remove();
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    }
                    else if (status == -1) //khong co trong danh sach buy
                        DisplayMessage(4, lblStockIsDeleteSuccessfully);
                    processing = false;
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
    //move stock
    $('body').on('click', '.contextMenu .movestock', function () {
        if (processing == false) {
            processing = true;
            var $element = $(this);
            var favID = $element.data('favid');
            $.ajax({
                url: "/Resources/ManagerFavorite.ashx",
                method: "POST",
                data: { stock: codeContext, type: 'movefavitem', favIDFrom: typeStock, favIDTo: favID },
                dataType: "json",
                success: function (response) {
                    var status = response.Status;
                    if (status == 1) { //add thanh cong
                        $(".contextMenu").finish().toggle(100);
                        $elementContext.remove();
                        DisplayMessage(4, String.format(lblStockIsMovedSuccessfullyTo, $element.text()));
                    }
                    else if (status == -1) //da co trong danh sach chuyen den
                        DisplayMessage(2, String.format(lblThisStockAlreadyExiston, $element.text()));
                },
                error: function (error) {
                    processing = false;
                }
            })
        }
    })
})

function OpenAddFavorite() {
    if (processing == false) {
        processing = true;
        //load danh sach favorite
        $.ajax({
            url: "/Resources/ManagerFavorite.ashx",
            method: "POST",
            data: { type: 'list' },
            dataType: "json",
            success: function (response) {
                var data = response.Data;
                var html = '';
                data.forEach(function (item) {
                    html += '<tr data-fid="' + item.FavID + '">';
                    html += '<td>' + item.FavName + '</td>';
                    html += '<td><i class="glyphicon glyphicon-remove" aria-hidden="true"></i></td>';
                    html += '</tr>';
                })
                $('#favList tbody').html(html);
                $(".modalFavorite").modal();
                processing = false;
            },
            error: function (error) {
                processing = false;
            }
        })
    }
}

function RefreshContext() {
    $.ajax({
        url: "/Resources/ManagerFavorite.ashx",
        method: "POST",
        data: { type: 'getcontext', typeStock: typeStock },
        dataType: "json",
        success: function (response) {
            $('ul.contextMenu').remove();
            $('body').append(response.Data);
            $('#dlStockType ul.dropdown-menu').html(response.DataMenu);
        }
    })

}

function CheckPrice(Price) {
    if (Price == -2 || Price == -1 || Price.toString() == 'NaN')
        Price = '-';
    return Price;
}

function DisplayMessage(type, message) {
    var typeText = 'info';

    if (type == 2) {
        typeText = 'warning';
    }
    else if (type == 3) {
        typeText = 'error';
    }
    else if (type == 4) {
        type = 'success';
    }
    $.toast({
        //heading: 'Positioning',
        text: message,
        position: 'top-center',
        icon: typeText,
        allowToastClose: true, // Boolean value true or false
        hideAfter: 3000,
    })
}